<?php 
    $collection = array();
    foreach ($this->food_ration_collection->toArray(true) as $food_intake) {
        $collection[$food_intake['food_intake_code']][] = 
            array_slice($food_intake, -3, 2);
    }
    
    // no table unless any fodd intake exists
    if (empty($collection)) : return; endif;
?>

<table id="content">
    <tr class="head">
        <td>Продукт</td>
        <td>Граммы</td>
        <?php 
        foreach($this->components as $code => $name) :
            ?><td><?=$name?></td><?php
        endforeach; 
        ?>
    </tr>
<?php
    static $glob_cach = array();
    
    foreach($collection as $food_intake_num => $food_intake) :
        ?><tr><?php
            ?><td colspan="7"><?=$food_intake_num?></td><?php
        ?></tr><?php
        
        static $interm_cach = array();
        
        foreach($food_intake as $product) :
        ?><tr><?php // class="<?=$this->cycle(array('odd', 'even'))->next()"    
            ?><td><?=$this->escape($product['Products']['name'])?></td><?php
            ?><td><?=$product['gramme']?></td><?php
            foreach($product['Products']['Content'] as $component) :
                // save component quantity
                if (array_key_exists($component['component_code'], $interm_cach)) {
                    $interm_cach[$component['component_code']] += 
                        $this->countComponent($component['value'], $product['gramme']);
                } else {
                    $interm_cach[$component['component_code']] = 
                        $this->countComponent($component['value'], $product['gramme']);
                }
                if (array_key_exists($component['component_code'], $glob_cach)) {
                    $glob_cach[$component['component_code']] += 
                        $this->countComponent($component['value'], $product['gramme']);
                } else {
                    $glob_cach[$component['component_code']] = 
                        $this->countComponent($component['value'], $product['gramme']);
                }
                ?><td><?=$this->countComponent($component['value'], $product['gramme'])?></td><?php
            endforeach;
        ?></tr><?php
        endforeach;
        ?><tr class="total"><?php
            ?><td colspan="2">Итого: </td><?php
            foreach($this->components as $code => $name) :
                ?><td><?=$interm_cach[$code]?></td><?php
            endforeach;
        ?></tr><?php
        // clear intermidiate cach
        $interm_cach = array();
        
    endforeach;
    
    ?><tr class="total"><?php
        ?><td colspan="2">Итого/Итого: </td><?php
        foreach($this->components as $code => $name) :
            ?><td><?=$glob_cach[$code]?></td><?php
        endforeach;
    ?></tr><?php
    
?>
</table>
